/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package cn.ac.iie.di.tools;

import cn.ac.iie.di.global.GlobalParas;
import static cn.ac.iie.di.gwxs.test.id2Provinces;
import cn.ac.iie.di.ipparser.RangeSearch;
import static cn.ac.iie.di.server.GwxParserStartup.errorLogger;
import java.util.Arrays;
import java.util.HashSet;
import org.apache.log4j.Logger;

/**
 *
 * @author Austin
 */
public class GetIpTools {

    private static Logger logger = Logger.getLogger(GetIpTools.class);

    private static final String provinceSplitter = "市";

    public static int[] getIpAddr(String ip) {
        String province = GlobalParas.rs1.getValue(ipToNumber(ip), "district");
//        logger.info("xxxxxxxxxxxxxxxxxxxxxxxx" + province);
        {
            String[] prs = province.split(provinceSplitter);
            HashSet<String> set = new HashSet<>();
            StringBuilder sb = new StringBuilder();
            for (String str : prs) {
                if (set.contains(str)) {
                } else {
                    set.add(str);
                    sb.append(str).append(provinceSplitter);
                }
            }
            if (!province.endsWith(provinceSplitter)) {
                sb.deleteCharAt(sb.length() - 1);
            }
            province = sb.toString();
        }
//        logger.info("bbbbbbbbbbbbbbbbbbbbbbbbbbb" + province);
        String country = GlobalParas.rs1.getValue(ipToNumber(ip), "country");
        int[] addrNum = new int[2];
        if (province == null || "".equals(province.trim()) || "00".equals(province.trim())) {
//                        log.warn(" get " + sendIp + " fail from locator  00 , and the country is " + GlobalParas.rs1.getValue(sendIp, "country") + " ip is " + sendIp);
            if (GlobalParas.id2Provinces.containsKey(country)) {
                addrNum[0] = GlobalParas.id2Provinces.get(country);
                if (GlobalParas.id2Provinces.get(country).toString().length() < 4) {
                    addrNum[1] = Integer.parseInt(GlobalParas.id2Provinces.get(country).toString());
                } else {
                    addrNum[1] = Integer.parseInt(GlobalParas.id2Provinces.get(country).toString().substring(0, 2));
                }
            } else {
                addrNum[0] = -1;
                addrNum[1] = -1;
            }
        } else if (GlobalParas.id2Provinces.containsKey(province.trim())) {
            int pro = GlobalParas.id2Provinces.get(province.trim());
            addrNum[0] = pro;
            addrNum[1] = Integer.parseInt(String.valueOf(pro).substring(0, 2));
        } else if (province.trim().endsWith("0")) {
//                        log.warn(province.trim());
            if (GlobalParas.id2Provinces.containsKey(province.trim().split("[0]")[0])) {
                int pro = GlobalParas.id2Provinces.get(province.trim().split("[0]")[0]);
                addrNum[0] = pro;
                addrNum[1] = Integer.parseInt(String.valueOf(pro).substring(0, 2));
            } else if (GlobalParas.id2Provinces.containsKey(country)) {
                addrNum[0] = GlobalParas.id2Provinces.get(country);
                addrNum[1] = Integer.parseInt(GlobalParas.id2Provinces.get(country).toString().substring(0, 2));
            } else {
                logger.warn("get " + ip + " fail from locator , and the country is " + country + " , the province is " + province);
                addrNum[0] = -1;
                addrNum[1] = -1;
            }

        } else {
            if (!"IANA".equals(country)) {
                logger.warn("get " + ip + " fail from locator , and the country is " + country + " , the province is " + province);
            }
            addrNum[0] = -1;
            addrNum[1] = -1;
//                        log.error("msg get null wb_nad : " + province);
        }
        if (addrNum[0] == -1 || addrNum[1] == -1) {
            if (!"0.0.0.0".equals(ip)) {
                errorLogger.error("unknow ip is :" + ip);
            }

        }
        return addrNum;
    }

    private static Long ipToNumber(String ip) {
        Long ips = 0L;
        String[] numbers = ip.split("\\.");
        //等价上面  
        for (int i = 0; i < 4; ++i) {
            ips = ips << 8 | Integer.parseInt(numbers[i]);
        }
        return ips;
    }

    public static void main(String[] args) {
        System.out.println(Arrays.asList(getIpAddr("103.95.95.4")));
    }
}
